package com.study.happystudy.service.impl;

import com.google.gson.Gson;
import com.study.happystudy.mapper.PostsMapper;
import com.study.happystudy.mapper.UserMapper;
import com.study.happystudy.po.Post;
import com.study.happystudy.po.Reply;
import com.study.happystudy.po.User;
import com.study.happystudy.service.PostsService;
import com.study.happystudy.tool.ColorChange;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PostsServiceImpl implements PostsService {
    @Resource
    PostsMapper p;
    @Resource
    UserMapper u;

    @Override
    public void addPost(int authorId, List<String> tags, String title, String content, List<String> images) {
        try {
            Gson gson = new Gson();
            String jsonTags = gson.toJson(tags);
            String jsonImages = gson.toJson(images);
            p.addPost(authorId, jsonTags, title, content, jsonImages);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Post getPostById(int postId) {
        return toPost(p.getPostById(postId));
    }

    @Override
    public void deletePostById(int postId) {
        p.deletePostById(postId);
    }

    @Override
    public List<Post> getPostsByUserId(int userId) {
        List<Post> ps = p.getPostsByUserId(userId);

        if (ps == null) return null;

        ps.replaceAll(this::toPost);

        return ps;
    }

    @Override
    public List<Reply> getRepliesByPostId(int postId) {

        List<Reply> list = p.getRepliesByPostId(postId);

        if(list == null) return null;

        try {
            list.replaceAll(this::toReply);
        }catch (Exception e){
            ColorChange.Red("Fuck: "+ e.getMessage());
        }

        return list;
    }

    @Override
    public List<Post> searchPosts(String keyword) {

        List<Post> posts = p.searchPosts(keyword);

        if(posts == null){
            return null;
        }

        try {
            posts.replaceAll(this::toPost);
        }catch (Exception e){
            ColorChange.Red("Fuck: "+ e.getMessage());
            return null;
        }

        return posts;
    }

    @Override
    public void likePost(int userId, int postId) {

        userView(userId, postId);

        p.likePost(userId, postId);
    }

    @Override
    public void unlikePost(int userId, int postId) {
        p.unlikePost(userId, postId);
    }

    @Override
    public void addReply(int postId, int replierId, String replyContent) {
        userView(replierId, postId);
        p.addReply(postId, replierId, replyContent);
    }

    @Override
    public void likeReply(int userId, int replyId) {
        p.likeReply(userId, replyId);
    }

    @Override
    public void unlikeReply(int userId, int replyId) {
        p.unlikeReply(userId, replyId);
    }

    @Override
    public boolean isUserLikePost(int userId, int postId) {
        return p.isUserLikePost(userId, postId);
    }

    @Override
    public boolean isUserLikeReply(int userId, int replyId) {
        return p.isUserLikeReply(userId, replyId);
    }

    @Override
    public Reply getReplyById(int replyId) {
        return p.getReplyById(replyId);
    }

    //todo: 下面懒得写service了，谁有空自己补上去
    public List<String> getLikeUserAvatars(int postId) {
        return p.getLikeUserAvatars(postId);
    }

    public int getCommentCountByPostId(int postId) {
        return p.getCommentCountByPostId(postId);
    }

    public void userView( int userId,  int postId) {

        if(isUserViewPost(userId, postId) == false){
            p.userView(userId, postId);
        }

    }

    public int getViewCountByPostId(int postId) {
        return p.getViewCountByPostId(postId);
    }

    public boolean isUserViewPost(int userId,int postId) {
        return p.isUserViewPost(userId, postId);
    }

    public void deleteReplyById(int replyId){
        p.deleteReplyById(replyId);
    }

    private Post toPost(Post post) {

        if(post == null) return null;

        int pid = post.getPostId();
        List<String> likeUserAvatars = p.getLikeUserAvatars(pid);

        // 稀饭头像、评论数量
        post.setLikeUserAvatars(likeUserAvatars);
        post.setLikeCount(likeUserAvatars.size());

        // 评论数量
        post.setCommentCount(p.getCommentCountByPostId(pid));

        // 浏览数量
        post.setViewCount(p.getViewCountByPostId(pid));

        return post;
    }

    private Reply toReply(Reply reply) {

        User r = u.findUserById(reply.getReplierId());

        try {
            reply.setNickname(r.getUserNickname());
            reply.setAvatar(r.getUserHeadPortrait());
        }
        catch (Exception e){
            ColorChange.Red("Fuck: "+ e.getMessage());
        }

        return reply;
    }

    public List<Post> getAllPosts(){
        try{
            List<Post> allPosts = p.getAllPosts();
            allPosts.replaceAll(this::toPost);
            return allPosts;
        }catch (Exception e){
            ColorChange.Red("Fuck: "+ e.getMessage());
        }
        return null;
    }

}
